package org.zoomdev.zoom.aop.utils;

import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;

/**
 * 将某个方法从源类中切割出来
 */
public class SplitMethodUtils {

    /**
     * @param srcBytes
     * @param newModuleClassName ModuleACreateA
     * @param methodName         createA
     * @param oldNew             new A
     * @param replaceNew         new ExtA
     * @return
     */
    public static byte[] split(byte[] srcBytes,
                               String newModuleClassName,
                               String methodName,
                               String oldNew,
                               String replaceNew) {

        ClassReader cr = new ClassReader(srcBytes);
        ClassWriter cw = new ClassWriter(cr, 0);
        cr.accept(new CopyMethodVisitor(cw,
                        AsmUtils.makeExternalName(newModuleClassName),
                        methodName,
                        AsmUtils.makeExternalName(oldNew),
                        AsmUtils.makeExternalName(replaceNew)
                ),
                0);

        return cw.toByteArray();
    }
}
